<?php

namespace Addons\IntegralShop\Repositor;

use Addons\IntegralShop\Model\IsSpreadsRecordModel;

class IsSpreadsRecordRepositor extends BaseRepositor
{
    const EVENT_TYPE_SPREAD = 1;
    const EVENT_TYPE_EXCHANGE_GOOD = 2;
    const EVENT_TYPE_RETURN = 3;

    const OP_TYPE_ADD = 1;
    const OP_TYPE_REDUCE = 2;

    static private $eventTypelabels = [
        self::EVENT_TYPE_SPREAD => "推广",
        self::EVENT_TYPE_EXCHANGE_GOOD => "兑换商品",
        self::EVENT_TYPE_RETURN => "系统返还",
    ];

    static private $opTypeLabels = [
        self::OP_TYPE_ADD => "添加",
        self::OP_TYPE_REDUCE => "减少",
    ];

    static private $opTypeLabelsHtml = [
        self::OP_TYPE_ADD => "<font color='green'>添加</font>",
        self::OP_TYPE_REDUCE => "<font color='red'>减少</font>",
    ];

    public static function eventType($type = null)
    {
        if ($type === null) {
            return self::$eventTypelabels;
        } else {
            return isset(self::$eventTypelabels[$type]) ? self::$eventTypelabels[$type] : "";
        }
    }

    public static function opType($type = null)
    {
        if ($type === null) {
            return self::$opTypeLabels;
        } else {
            return isset(self::$opTypeLabels[$type]) ? self::$opTypeLabels[$type] : "";
        }
    }

    public static function opTypeHtml($type = null)
    {
        if ($type === null) {
            return self::$opTypeLabelsHtml;
        } else {
            return isset(self::$opTypeLabelsHtml[$type]) ? self::$opTypeLabelsHtml[$type] : "";
        }
    }

    private $model;

    public function __construct()
    {
        $this->model = new IsSpreadsRecordModel();
    }

    public function add($openId, $eventType, $eventContent, $markOpType, $mark)
    {
        return $this->model->add([
            'mpid' => get_mpid(),
            'openid' => $openId,
            'event_type' => $eventType,
            'event_content' => $eventContent,
            'mark_op_type' => $markOpType,
            'mark' => $mark,
            'created_at' => commonTimeFormat()
        ]);
    }

    public function userlist()
    {
        return $this->model->where([
            'openid' => get_openid()
        ])->order('id desc')->select();
    }

    public function getSpreadNum($openid){
        return $this->model->where(['openid'=>$openid,'event_type'=>self::EVENT_TYPE_SPREAD])->count();
    }
}
